#ifdef MAODV

/*****************************************************************************
 *
 * Copyright (C) 2003 CSHCN, University of Maryland.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors: Dionysus Blazakis <dblaze@umd.edu>
 *          Jonathan Polley <jpolley@umd.edu>
 *          Dan Rusk <drusk@umd.edu>
 *          Manish Karir <karir@umd.edu>
 *
 *****************************************************************************/

#include "defs.h"
#include "debug.h"
#include "maodv_rrep.h"
#include "routing_table.h"
#include "multicast_routing_table.h"
#include "aodv_socket.h"
#include "aodv_rreq.h"
#include "aodv_rrep.h"
#include "packet_queue.h"

/*
 *  maodv_rrep_process( lots of stuff )
 *  Processes an rrep that has already been sanity checked and is
 *  responding to a multicast group.
 */
void maodv_rrep_process( RREP *rrep, int rreplen, u_int32_t ip_src,  
    u_int32_t ip_dst, int ip_ttl, unsigned int ifindex, int size )
{
    mrt_nexthop_t * upstream;
    mrt_table_t * mrt;
    rt_table_t * rev_rt;
    u_int32_t rrep_orig, rrep_dest, rrep_seqno, rrep_lifetime;
    mrt_orig_t *orig;
    
    DEBUG( LOG_DEBUG, 0, "Inside RREP process." );

    rrep_dest = ntohl(rrep->dest_addr);
    rrep_seqno = ntohl(rrep->dest_seqno);
    rrep_orig = ntohl(rrep->orig_addr);
    rrep_lifetime = ntohl(rrep->lifetime);

    rev_rt = rt_table_find( rrep_orig );
    mrt = mrt_table_find( rrep_dest );

    if ( !mrt ) {
        log(LOG_WARNING, 0, __FUNCTION__,
            "No multicast route to group %s found in RREP.",
            ip_to_str( rrep_dest ) );
        return;
    }

    upstream = mrt_nexthop_find(mrt, ip_src);
    if(upstream == NULL){
        DEBUG( LOG_DEBUG, 0, "Created nexthop." );
        upstream = mrt_nexthop_add( mrt, ip_src, MRT_UPSTREAM, rrep_seqno );
    } else {
        /* update seqno */
        DEBUG( LOG_DEBUG, 0, "Had nexthop, updated seqno." );
        upstream->grp_seqno = rrep_seqno;
    }
    
    /* add the originator to the originator list if not there already */
    orig=mrt_origlist_find(mrt, rrep_orig);
    if(orig==NULL){
        DEBUG(LOG_DEBUG, 0, "originator not in list, adding");
        orig=mrt_origlist_add(mrt, rrep_orig);
    }
 
    DEBUG(LOG_DEBUG, 0, "originator seqno: %d hcnt: %d", orig->grp_seqno, 
        orig->grp_hcnt);

    /* rrep is propagated if the prior rreq had a lower
       sequence number or the sequence numbers are equal and the
       incoming rrep has a smaller hopcount */ 
    if ( ( ( orig->grp_seqno < rrep_seqno ) ||
           ( ( orig->grp_seqno == rrep_seqno ) &&
             ( orig->grp_hcnt > rrep->hcnt ) ) ) ){

        DEBUG( LOG_DEBUG, 0, "Forwardable/acceptable rrep." );

        mrt->grp_seqno = rrep_seqno;
        orig->grp_seqno=rrep_seqno;
        orig->grp_hcnt = rrep->hcnt;

        if ( !upstream->activated ) {
            upstream->direction = MRT_UPSTREAM;
            upstream->grp_hcnt = rrep->hcnt;
        }
        
        if ( rrep_orig != DEV_IFINDEX(ifindex).ipaddr )
            maodv_rrep_forward( rrep, rev_rt, mrt, --ip_ttl, size );
    } else {
        DEBUG( LOG_DEBUG, 0,"mrt->grp_seqno: %d\trrep_seqno: %d", 
            mrt->grp_seqno, rrep_seqno );
    }
}

void maodv_rrep_forward( RREP * rrep, rt_table_t * rev_rt,
    mrt_table_t * fwd_rt, int ttl, int size )
{
    mrt_nexthop_t * upstream;

    if (!fwd_rt || !rev_rt) {
        DEBUG(LOG_WARNING, 0, "Could not forward RERR because of NULL route!");
        return;
    }

    if (!rrep) {
        DEBUG(LOG_WARNING, 0, "No RREP to forward!");
        return;
    }

    DEBUG(LOG_DEBUG, 0, "Forwarding RREP to %s", ip_to_str(rev_rt->next_hop));

    rrep = (RREP *) aodv_socket_queue_msg((AODV_msg *) rrep, size);
    upstream = mrt_table_get_best_upstream( fwd_rt );
    
    /* Update the hopcount */
    if(!fwd_rt->is_router){
        rrep->hcnt++;
    }

    aodv_socket_send((AODV_msg *) rrep, rev_rt->next_hop, size, ttl,
                     &DEV_IFINDEX(rev_rt->ifindex));

    /* TODO: timing code... */
/*    rt_table_update_timeout(rev_rt, ACTIVE_ROUTE_TIMEOUT); */
}

void maodv_rrep_send( rt_table_t * rev_rt, mrt_table_t * fwd_rt )
{
    RREP *rrep;
    u_int8_t rrep_flags = 0;
    u_int32_t dest, lifetime;
    int hcnt;

    /* CHECK: we do not use this... */
    lifetime = 0x0ffffffff;

    if (!rev_rt) {
        DEBUG(LOG_WARNING, 0, "Can't send RREP, rev_rt = NULL!");
        return;
    }

    if (!fwd_rt) {
        DEBUG(LOG_WARNING, 0, "Can't send RREP, fwd_rt = NULL!");
        return;
    }

    DEBUG(LOG_DEBUG, 0, "[maodv] sending rrep.");

    dest = fwd_rt->grp_addr;

    hcnt = 0;

    /* TODO: timeout code... */
    /*
    gettimeofday(&now, NULL);
    lifetime = timeval_diff(&fwd_rt->rt_timer.timeout, &now);
    */

    DEBUG(LOG_DEBUG, 0, "[maodv] Sending RREP to %s about %s->%s",
          ip_to_str(rev_rt->next_hop), ip_to_str(rev_rt->dest_addr),
          ip_to_str(dest));

    rrep = rrep_create(rrep_flags, 0, hcnt, dest, fwd_rt->grp_seqno, 
        rev_rt->dest_addr, lifetime, 0 );
        
    aodv_socket_send((AODV_msg *) rrep, rev_rt->next_hop, RREP_SIZE, MAXTTL,
                     &DEV_IFINDEX(rev_rt->ifindex));

}

#endif /* MAODV */
